package com.fosss.tree;

/**
 * 二叉树的前序中序后序遍历
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode root=new HeroNode(1,"松江");
        HeroNode left1=new HeroNode(2,"武松");
        HeroNode right1=new HeroNode(3,"吴用");
        HeroNode 关胜 = new HeroNode(4, "关胜");
        HeroNode 李逵 = new HeroNode(5, "李逵");

        root.setLeft(left1);
        root.setRight(right1);
        right1.setLeft(关胜);
        right1.setRight(李逵);
        binaryTree.setRoot(root);

        System.out.println("遍历！！！！！！！！！！！！！！！");
        System.out.println("前序遍历");
        binaryTree.preOrder();
        System.out.println("\n中序遍历");
        binaryTree.middleOrder();
        System.out.println("\n后序遍历");
        binaryTree.lastOrder();
        System.out.println("----------------------------");

        System.out.println("查找！！！！！！！！！！！！！！！");
        System.out.println("前序："+binaryTree.preSearch(6));
        System.out.println("中序："+binaryTree.middleSearch(5));
        System.out.println("后序："+binaryTree.lastSearch(5));
        System.out.println("----------------");

        System.out.println("删除");
        binaryTree.deleteByNo(3);
        binaryTree.preOrder();
    }

}

/**
 * 创建二叉树类
 */
class BinaryTree{
    private HeroNode root;
    private HeroNode left;
    private HeroNode right;

    public BinaryTree(){}
    public BinaryTree(HeroNode root, HeroNode left, HeroNode right) {
        this.root = root;
        this.left = left;
        this.right = right;
    }

    public HeroNode getRoot() {
        return root;
    }

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    //前序遍历
    public void preOrder(){
        if(this.root!=null){
            this.root.preOrder();
        }
    }

    //中序遍历
    public void middleOrder(){
        if(this.root!=null){
            this.root.middleOrder();
        }
    }
    //后序遍历
    public void lastOrder(){
        if(this.root!=null){
            this.root.lastOrder();
        }
    }

    //前序遍历查找
    public HeroNode preSearch(int no){
        if(this.root==null) {
            return null;
        }
        return this.root.preSearch(no);
    }
    //中序遍历查找
    public HeroNode middleSearch(int no){
        if(this.root==null) {
            return null;
        }
        return this.root.middleSearch(no);
    }
    //后序遍历查找
    public HeroNode lastSearch(int no){
        if(this.root==null) {
            return null;
        }
        return this.root.lastSearch(no);
    }

    /**
     * 删除指定节点
     */
    public void deleteByNo(int no){
        if(this.root==null) {
            return;
        }
        if(this.root.getNo()==no){
            this.root=null;
            return;
        }
        this.root.deleteByNo(no);
    }
}


/**
 * 创建结点类
 */
class HeroNode{
    private int no;
    private String name;
    private HeroNode left;//默认为空
    private HeroNode right;//默认为空

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //前序遍历
    public void preOrder(){
        //先输出当前节点，再向左递归，再向右递归
        System.out.println(this);//谁调用这个方法,this就代表谁
        if(this.left!=null){
            this.left.preOrder();
        }
        if(this.right!=null){
            this.right.preOrder();
        }
    }
    //中序遍历
    public void middleOrder(){
        //先向左递归，再输出当前节点，再向右递归
        if(this.left!=null){
            this.left.middleOrder();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.middleOrder();
        }
    }
    //后序遍历
    public void lastOrder(){
        //先向左递归，再向右递归,再输出当前节点
        if(this.left!=null){
            this.left.lastOrder();
        }
        if(this.right!=null){
            this.right.lastOrder();
        }
        System.out.println(this);
    }

    //前序遍历查找
    public HeroNode preSearch(int no){
        //先判断当前结点是不是，不是的话向左递归，向右递归
        HeroNode heroNode=null;

        if(this.no==no){
            return this;
        }
        if(this.left!=null){
            heroNode=this.left.preSearch(no);
        }
        if(heroNode!=null){//这一步的作用是如果找到，就停止继续查找
            return heroNode;
        }

        if (this.right!=null){
            heroNode=this.right.preSearch(no);
        }
        return heroNode;

    }

    //中序遍历查找
    public HeroNode middleSearch(int no){
        //先判断当前结点是不是，不是的话向左递归，向右递归
        HeroNode heroNode=null;

        if(this.left!=null){
            heroNode=this.left.preSearch(no);
        }
        if(heroNode!=null){//这一步的作用是如果找到，就停止继续查找
            return heroNode;
        }
        if(this.no==no){
            return this;
        }
        if (this.right!=null){
            heroNode=this.right.preSearch(no);
        }
        return heroNode;
    }

    //后序遍历查找
    public HeroNode lastSearch(int no) {
        //先判断当前结点是不是，不是的话向左递归，向右递归
        HeroNode heroNode = null;

        if (this.left != null) {
            heroNode = this.left.preSearch(no);
        }
        if(heroNode!=null){//这一步的作用是如果找到，就停止继续查找
            return heroNode;
        }

        if (this.right != null) {
            heroNode = this.right.preSearch(no);
        }
        if (this.no == no) {
            return this;
        }
        return heroNode;
    }

    /**
     * 删除  如果是叶子节点就删除该节点，如果是子树，直接将子树删掉了
     */
    public void deleteByNo(int no){

        if(this.left!=null&&this.left.no==no){
            this.left=null;
            return;
        }
        if(this.right!=null&&this.right.no==no){
            this.right=null;
            return;
        }

        //向左递归
        if(this.left!=null){
            this.left.deleteByNo(no);
        }
        //向右递归
        if(this.right!=null){
            this.right.deleteByNo(no);
        }
    }
}



























